import * as request from "../services/CommonHttpService";
import PageHelper from "utils/pageHelper";
import $$ from 'utils';


export function run(runable) {
  while (true) {
    const {value, done} = runable.next();
    if (done) {
      return;
    }
  }
}

export const baseState = {
  page: {},
  loading: false,     // 加载数据
  changing: false,    // 增加和修改数据
  all: {},            // 加载所有数据
  loadingAll: false,
}

export const baseReducers = {

  initState: (state, action) => {
    return {...state, ...action.payload};
  },

  list: (state, action) => {
    $$.debug("表格显示的数据:", action.payload.data);
    return {...state, page: action.payload.data, loading: false};
  },

  loading: (state, action) => {
    return {...state, loading: true};
  },

  loadError: (state, action) => {
    return {...state, loading: false, page: {}};
  },

  loadingAll: (state, action) => {
    return {...state, loadingAll: true};
  },

  all: (state, action) => {
    return {...state, all: action.payload.data, loadingAll: false};
  },

  loadAllError: (state, action) => {
    return {...state, loadingAll: false, all: {}};
  },

  changing: (state, action) => {
    return {...state, changing: true};
  },

  changError: (state, action) => {
    return {...state, changing: false};
  },

  changSucess: (state, action) => {
    return {...state, changing: false};
  },

}

export const baseEffects = {
  // 进入页面加载
  * baseInit({payload}, {call, put, select}) {
    yield put({
      type: 'getPageInfo',
      payload: {
        page: {pageNum: 1, pageSize: 10},
        notice: payload.notice,
      }
    });
  },

  * baseGetAll({payload}, {call, put}) {

    yield put({type: 'loadingAll'});

    const {errorMsg, process, baseUrl, notice, server} = payload;

    try {
      let result = yield call(request.commonGet, baseUrl+'/all', {}, {}, server);

      !!process && (result = process(result));
      yield put({
        type: 'all',
        payload: {
          data: result
        }
      });
    } catch (error) {
      console.error("baseGetAll", error);
      yield put({type: 'loadAllError'});
      !!notice && notice("error", !!errorMsg ? errorMsg : error);
    }

  },

  * baseGetPageInfo({payload}, {call, put}) {

    yield put({type: 'loading'});

    const {page, sorter, errorMsg, process, baseUrl, notice, server} = payload;
    let {filters} = payload;

    if (!!filters) {
      const time = !!filters.time ? filters.time : [];
      if (time.length == 2) {
        const startTime = !!time[0] && time[0].format('x');
        const endTime = !!time[1] && time[1].format('x');
        if (!!startTime && !!endTime)
          filters = {...filters, startTime, endTime};
      }

      payload = {...payload, filters};

      delete filters.time;
      $$.removeNullItem(filters);
    }


    try {
      let result = yield call(request.commonGet, baseUrl, {},
        {...PageHelper.changePageParam(page), ...filters, sorter: (!!sorter ? JSON.stringify(sorter) : '{}')}, server);

      !!process && (result.records = process(result.records));
      yield put({
        type: 'list',
        payload: {
          data: PageHelper.changePage(result)
        }
      });
    } catch (error) {
      console.error("baseGetPageInfo", error);
      yield put({type: 'loadError'});
      !!notice && notice("error", !!errorMsg ? errorMsg : error);
    }

  },

  * baseSave({payload}, {call, put}) {
    const {value, success, baseUrl, errorMsg, notice, server} = payload;

    const http = !!value.id ? request.commonPut : request.commonFormPost;
    const url = !value.id ? baseUrl : `${baseUrl}/${value.id}`;

    console.log("baseSave", "payload", payload, url);

    yield put({type: 'changing'});

    try {
      yield call(http, url, {}, value, server);
      yield put({type: 'changSucess'});
      success();
    } catch (error) {
      console.error("baseSave", error);
      yield put({type: 'changError'});
      !!notice && notice("error", !!errorMsg ? errorMsg : error);
    }
  },

  * baseDelete({payload}, {call, put, select}) {
    const {values, success, baseUrl, errorMsg, notice, server} = payload;

    let ids = values.map(v => v.id);
    try {
      yield call(request.commonDelete, baseUrl, {}, ids, server);
      success();
    } catch (error) {
      console.error("baseDelete", error);
      !!notice && notice("error", !!errorMsg ? errorMsg : error);
    }
  },

  // 进入页面加载
  * init({payload}, {put}) {
    yield put({
      type: 'baseInit',
      payload
    });
  },

}

export function processFilter(filters) {

  if (!!filters) {
    const time = !!filters.time ? filters.time : [];
    if (time.length == 2) {
      const startTime = !!time[0] && time[0].format('x');
      const endTime = !!time[1] && time[1].format('x');
      if (!!startTime && !!endTime)
        filters = {...filters, startTime, endTime};
    }

    delete filters.time;
    $$.removeNullItem(filters);
  }
}

